package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.BpAlarmHistoryLog;
import com.example.service.IBpAlarmHistoryLogService;
import com.example.service.IBpDeviceHistoryLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ysr_xian
 * @since 2022-05-29
 */
@RestController
@RequestMapping("/bpAlarmHistoryLog")
public class BpAlarmHistoryLogController {

    @Autowired
    private IBpAlarmHistoryLogService bpAlarmHistoryLogService;

    @Autowired
    private IBpDeviceHistoryLogService bpDeviceHistoryLogService;

    @GetMapping("/list")
    public IPage<BpAlarmHistoryLog> list(@RequestParam(defaultValue = "device1") String deviceName,
                                         @RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize) {
        QueryWrapper<BpAlarmHistoryLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_name", deviceName); // 条件：名字相等
        queryWrapper.orderByDesc("gmt_create");  // 按日期升序
        return bpAlarmHistoryLogService.page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    @PostMapping("/delete")
    public boolean delete(@RequestBody BpAlarmHistoryLog bpAlarmHistoryLog) {
        bpDeviceHistoryLogService.removeById(bpAlarmHistoryLog.getBpDeviceId());
        return bpAlarmHistoryLogService.removeById(bpAlarmHistoryLog.getId());
    }
}

